package com.dotflat.photoAlbum.controller;

import com.dotflat.photoAlbum.domain.PhotoAlbumStatic;
import com.dotflat.photoAlbum.service.PhotoAlbumStaticPageService;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/api/static_photo_album")
@Slf4j
@Api(tags = "静态相册 - ")
public class PhotoAlbumStaticPageController {

    @Autowired
    private PhotoAlbumStaticPageService photoAlbumService;

    @RequestMapping(value = "/albums", method = RequestMethod.GET)
    public StatusResponse childrenAlbumPage(HttpServletRequest req, HttpServletResponse resp,
                                            @RequestHeader(TICKET) String ticket,
                                            String childrenId, PagerQuery pagerQuery ){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        Page<PhotoAlbumStatic> page = photoAlbumService.childrenAlbumPage(childrenId, defaultBaby.getAreaId(), pagerQuery);
        return StatusResponse.success(page);
    }

    @RequestMapping(value = "/request_print", method = RequestMethod.GET)
    public StatusResponse requestPrint(HttpServletRequest req, HttpServletResponse resp,
                                       @RequestHeader(TICKET) String ticket, String albumId ){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        String areaId = defaultBaby.getAreaId();

        PhotoAlbumStatic album = photoAlbumService.requestPrintAlbum(areaId, albumId);
        return StatusResponse.success(album);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public StatusResponse albumDetail(HttpServletRequest req, HttpServletResponse resp,
                                      @RequestHeader(TICKET) String ticket, String albumId ){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        String areaId = defaultBaby.getAreaId();

        PhotoAlbumStatic album = photoAlbumService.photoAlbumById(areaId, albumId);
        return StatusResponse.success(album);
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public StatusResponse createAlbum(HttpServletRequest req, HttpServletResponse resp,
                                      @RequestHeader(TICKET) String ticket, String childrenId, String jsonStr){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        PhotoAlbumStatic album = photoAlbumService.createPhotoAlbum(childrenId, defaultBaby.getAreaId(), defaultBaby.getAreaName(),jsonStr);
        return StatusResponse.success(album);
    }

    @RequestMapping(value = "/album_templates", method = RequestMethod.GET)
    public StatusResponse getAlbumTemplates(HttpServletRequest req, HttpServletResponse resp,
                                            @RequestHeader(TICKET) String ticket, PagerQuery pagerQuery ){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        String areaId = defaultBaby.getAreaId();

        List templates = photoAlbumService.getAllTemplates(areaId);
        return StatusResponse.success(templates);
    }

    @RequestMapping(value = "/album_template_pages", method = RequestMethod.GET)
    public StatusResponse getAlbumTemplatePages(HttpServletRequest req, HttpServletResponse resp,
                                                @RequestHeader(TICKET) String ticket, String templateId ){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        String areaId = defaultBaby.getAreaId();

        List pages = photoAlbumService.getAllTemplatePages(templateId);
        return StatusResponse.success(pages);
    }
}
